﻿/// <reference path="../../../../Scripts/jqGrid/jqGrid-4.6.0/i18n/grid.locale-en.js" />
/// <reference path="../../../../Scripts/jqGrid/jqGrid-4.6.0/jquery.jqGrid.src.js" />
/// <reference path="../../../../Scripts/jQuery/jquery-1.11.0.js" />
/// <reference path="../../../../Scripts/jQuery-UI/jquery-ui-1.10.4.custom.js" />
/// <reference path="../../../../Scripts/vpGrid/vpGrid-0.6.9.js" />



(function (window, $, vpGrid, undefined) {
    var MyCompany = window.MyCompany || {};
    MyCompany.MyGrid = MyCompany.MyGrid || {};
    var MyGrid = MyCompany.MyGrid;
    try {
        

        onInlineSummaryRowRenderingEventhandler = function (source, eventArgs) {
            try {
                var rowid = eventArgs.rowid;
                var rowData = eventArgs.rowData;

                

            } catch (e) {
                vpGrid.logException(e);
            }
        }

        onSummaryCellRenderingEventhandler = function(source, eventArgs){
            try {
                var colName = eventArgs.cellName;
                if (colName === "price" || colName === "safetyStockLevel"
                    ) {
                    //var result = vpGrid.sumOf(colName);

                    //async version

                    eventArgs.asyncInit();

                    vpGrid.asyncFilteredSumOf(colName, function (row, data) {
                        try {
                            // return true if the row must be selected for the sum count
                            // return false if the row must not be selected upon business rules based on the data content (literal object representing the row)
                            return true;

                        } catch (e) {
                            vpGrid.logException(e);
                            return false;
                        }

                    }, 
                    true,
                    function (result) {
                        try {
                            eventArgs.cellValue = result;
                            eventArgs.useCellFormatter = true;
                            eventArgs.asyncEnd();
                        } catch (e) {

                        }


                        
                    }
                    );


                    
                }
            } catch (e) {
                vpGrid.logException(e);
            }
        }

        onDataReceivedEventhandler = function (source, eventArgs) {
            try {
                var data = eventArgs.data;
                var itemCount = data.length;
                for (var i = 0; i < itemCount; i++) {
                    var item = data[i];
                    if (item.style === "U") {
                        item.style = "Universal";
                        continue;
                    }
                    if (item.style === "W") {
                        item.style = "Women's";
                        continue;
                    }

                    if (item.style === "M") {
                        item.style = "Men's";
                        continue;
                    }


                }
                
            } catch (e) {
                vpGrid.logException(e);
            }
        }

        onUpdatedDataReceivedEventhandler = function (source, eventArgs) {
            try {
                var data = eventArgs.data;
                
                
            } catch (e) {
                vpGrid.logException(e);
            }
        }

        onInsertedDataReceivedEventhandler = function (source, eventArgs) {
            try {
                var data = eventArgs.data;


            } catch (e) {
                vpGrid.logException(e);
            }
        }

        onCellUpdatedEventhandler = function (source, eventArgs) {
            try {
                var colName = eventArgs.cellName;
                var data = eventArgs.rowData;
                if (colName === "price" ||colName === "safetyStockLevel") {
                    data.safetyStockValue =data.price.multipliedBy( data.safetyStockLevel);
                }
            } catch (e) {
                vpGrid.logException(e);
            }
        }

        onCellValidatingEventhandler = function (source, eventArgs) {
            try {
                var colName = eventArgs.cellName;
                var cellValue = eventArgs.cellValue;
                var cellNativeValue = eventArgs.cellNativeValue;
                
                if (colName === "price" && cellNativeValue === 9.99) {
                    eventArgs.isValid = false; 
                    eventArgs.errorMessage = "9.99 is forbidden bla1 bla2 xxxxxxx xxxxxxx xxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxx xxxxxxx xxxxxxxxx xxxxxxxxx yyyyyyyyy yyyyyyyyyyy yyyyyy yyyy yyyyy";
                }

                //if (colName === "startDate" ) {
                //    eventArgs.isValid = false;
                //    eventArgs.errorMessage = "this date is forbidden";
                //}

                if (colName === "name" && (cellValue === null || cellValue === '' || cellValue === undefined )) {
                    eventArgs.isValid = false;
                    eventArgs.errorMessage = "Name is required";
                }

            } catch (e) {
                vpGrid.logException(e);
            }
        }

        onRowEditingEventhandler = function (source, eventArgs) {
            try {
                var rowId = eventArgs.rowId;

                if (rowId === "1") {
                    eventArgs.cancel = true;
                    eventArgs.cancelReason = "You do not have permission to edit this row";
                }
            } catch (e) {
                vpGrid.logException(e);
            }
        };

        onCellEditingEventhandler = function (source, eventArgs) {
            try {
                var rowId = eventArgs.rowId;
                var cellName = eventArgs.cellName;

                if (rowId === "3" && cellName === "name") {
                    eventArgs.cancel = true;
                    eventArgs.cancelReason = "You do not have permission to edit the name of the product";
                }
            } catch (e) {
                vpGrid.logException(e);
            }
        };

        vpGrid.registerEventHandler(vpGrid.Event.onSummaryCellRendering, onSummaryCellRenderingEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onDataReceived, onDataReceivedEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onCellUpdated, onCellUpdatedEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onCellValidating, onCellValidatingEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onRowEditing, onRowEditingEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onCellEditing, onCellEditingEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onInlineSummaryRowRendering, onInlineSummaryRowRenderingEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onUpdatedDataReceived, onUpdatedDataReceivedEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onInsertedDataReceived, onInsertedDataReceivedEventhandler);
        
    } catch (e) {
        vpGrid.logException(e);
    }
    finally {
        window.MyCompany = MyCompany;
    }


})(this, jQuery, vpGrid);
